package com.jia.lidao.ui.personal;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.jia.lidao.R;
import com.jia.lidao.UserUtils;
import com.jia.lidao.enity.AgentRespDTO;
import com.jia.lidao.enity.LoginRespDTO;
import com.jia.lidao.network.http.BusinessManager;
import com.jia.lidao.network.http.MyCallback;
import com.jia.lidao.network.http.request.impl.AdminRequest;
import com.jia.lidao.network.http.request.impl.RefreshTokenRequest;
import com.jia.lidao.others.AddressActivity;
import com.jia.lidao.others.ui.ClassifyActivity;
import com.jia.lidao.others.ui.MineCollectActivity;
import com.jia.lidao.others.ui.MyOrderActivity;
import com.jia.lidao.others.ui.ShoppingCartActivity;
import com.jia.lidao.ui.BaseActivity;
import com.jia.lidao.ui.WebActivity;
import com.jia.lidao.ui.message.MessageNoticeActivity;
import com.jia.lidao.ui.order.OrderList2Activity;
import com.jia.lidao.ui.out.SettingActivity;
import com.jia.lidao.utils.ImgLoaderUtil;
import com.jia.lidao.utils.LogUtil;
import com.jia.lidao.widget.RoundImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;

/**
 * Created by jia on 17/12/16.
 */

public class PersonalActivity extends BaseActivity implements View.OnClickListener {


    private TextView nameTxt, areaTxt, roleTxt, unreadnumTxt;

    private RoundImageView headImg;

    private DisplayImageOptions options;

    private TextView totalloanTxt, pendingLoanTxt, passedLoanTxt, refusedLoanTxt;

    private TextView balanceTxt, incomeTxt, loanTxt, withdrawTxt;

    private Intent intent;

    private int type;



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personal);
        options = new DisplayImageOptions.Builder().showImageForEmptyUri(R.drawable.default_img).showImageOnFail(R.drawable.default_img).showImageOnLoading(R.drawable.default_img).build();
        initView();
    }


    private void initView() {
        headImg = (RoundImageView) findViewById(R.id.personal_head_img);
        nameTxt = (TextView) findViewById(R.id.personal_name_txt);
        areaTxt = (TextView) findViewById(R.id.personal_area_txt);
        roleTxt = (TextView) findViewById(R.id.personal_role_txt);
        unreadnumTxt = (TextView) findViewById(R.id.personal_messagenum_txt);
        balanceTxt = (TextView) findViewById(R.id.personal_wallet_balance_txt);
        loanTxt = (TextView) findViewById(R.id.personal_wallet_credit_txt);
        incomeTxt = (TextView) findViewById(R.id.personal_wallet_income_txt);
        withdrawTxt = (TextView) findViewById(R.id.personal_wallet_withdraw_txt);

        totalloanTxt = (TextView) findViewById(R.id.personal_all_txt);
        pendingLoanTxt = (TextView) findViewById(R.id.personal_inreview_txt);
        passedLoanTxt = (TextView) findViewById(R.id.personal_review_pass_txt);
        refusedLoanTxt = (TextView) findViewById(R.id.personal_review_feedback_txt);
        findViewById(R.id.personal_order_ll).setOnClickListener(this);
        findViewById(R.id.personal_all_ll).setOnClickListener(this);
        findViewById(R.id.personal_inreview_ll).setOnClickListener(this);
        findViewById(R.id.personal_review_pass_ll).setOnClickListener(this);
        findViewById(R.id.personal_review_feedback_ll).setOnClickListener(this);
        findViewById(R.id.personal_wallet_balance_ll).setOnClickListener(this);
        findViewById(R.id.personal_wallet_withdraw_ll).setOnClickListener(this);
        findViewById(R.id.personal_wallet_income_ll).setOnClickListener(this);
        findViewById(R.id.personal_wallet_credit_ll).setOnClickListener(this);
        findViewById(R.id.personal_want_order_ll).setOnClickListener(this);
        findViewById(R.id.personal_my_order_ll).setOnClickListener(this);
        findViewById(R.id.personal_shop_inform_ll).setOnClickListener(this);
        findViewById(R.id.personal_good_manage_ll).setOnClickListener(this);
        findViewById(R.id.personal_proxy_list_ll).setOnClickListener(this);
        findViewById(R.id.personal_add_proxy_ll).setOnClickListener(this);
        findViewById(R.id.personal_ordercar_ll).setOnClickListener(this);
        findViewById(R.id.personal_address_ll).setOnClickListener(this);
        findViewById(R.id.personal_mycollection_ll).setOnClickListener(this);
        findViewById(R.id.personal_gift_ll).setOnClickListener(this);
        findViewById(R.id.personal_phone_ll).setOnClickListener(this);
        findViewById(R.id.personal_sort_ll).setOnClickListener(this);
        findViewById(R.id.personal_wait_pay_ll).setOnClickListener(this);
        findViewById(R.id.personal_wait_shipments_ll).setOnClickListener(this);
        findViewById(R.id.personal_wait_receive_ll).setOnClickListener(this);
        findViewById(R.id.personal_completed_ll).setOnClickListener(this);
        findViewById(R.id.setting).setOnClickListener(this);
        findViewById(R.id.message).setOnClickListener(this);
    }

    private void initData() {
            BusinessManager.getInstance().getAgent(new AdminRequest(), new MyCallback<AgentRespDTO>() {
                @Override
                public void onSuccess(AgentRespDTO result, String message) {
                    if (result == null || result.agent == null) {
                        showResultErrorToast();
                        return;
                    }
                    ImgLoaderUtil.displayImage(result.agent.avatar, headImg, options);
                    nameTxt.setText(result.agent.agent_name);
                    result.agent.merchant_id=result.merchant_id;
                    roleTxt.setText("账户角色: " + result.agent.type_name);
                    UserUtils.getInstance().saveAgent(result.agent);
                    if (result.agent.message_count != 0) {
                        unreadnumTxt.setVisibility(View.VISIBLE);
                        unreadnumTxt.setText(result.agent.message_count + "");
                    } else {
                        unreadnumTxt.setVisibility(View.GONE);
                    }
                    type = result.agent.type;
                    if (type == 1) {
                        findViewById(R.id.personal_order_part).setVisibility(View.GONE);
                        ((TextView) findViewById(R.id.personal_order_manger_txt)).setText("订货列表");
                        ((TextView) findViewById(R.id.personal_my_order_txt)).setText("订货审核");
                    } else if (type == 2) {
                        findViewById(R.id.personal_order_part).setVisibility(View.VISIBLE);
                        findViewById(R.id.personal_proxy_ll).setVisibility(View.GONE);
                        findViewById(R.id.personal_sort_ll).setVisibility(View.INVISIBLE);
                    }
                    areaTxt.setText("所属区域: " + result.agent.area.name);
                    totalloanTxt.setText(result.agent.total_loan + "");
                    passedLoanTxt.setText(result.agent.passed_loan_count + "");
                    pendingLoanTxt.setText(result.agent.pending_audit_loan_count + "");
                    refusedLoanTxt.setText(result.agent.refused_loan_count + "");

                    balanceTxt.setText(result.agent.balance + "");
                    incomeTxt.setText(result.agent.commission + "");
                    loanTxt.setText(result.agent.loan_amount + "");
                    withdrawTxt.setText(result.agent.balance + "");
                }

                @Override
                public void onError(String errorCode, String message) {

                }

                @Override
                public void onFinished() {

                }

                @Override
                public void onRefreshToken() {
                    UserUtils.getInstance().goToLogin(PersonalActivity.this);
                }
            });
        }


    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.personal_order_ll:
                startActivity(new Intent(PersonalActivity.this, OrderList2Activity.class));
                break;
            case R.id.personal_all_ll:
                intent = new Intent(PersonalActivity.this, WebActivity.class);
                intent.putExtra(WebActivity.INTENT_EXTRA_BEGIN_URL, "loansaduitlist");
                startActivity(intent);
                break;
            case R.id.personal_inreview_ll:
                intent = new Intent(PersonalActivity.this, WebActivity.class);
                intent.putExtra(WebActivity.INTENT_EXTRA_BEGIN_URL, "loansaduitwait");
                startActivity(intent);
                break;
            case R.id.personal_review_pass_ll:
                intent = new Intent(PersonalActivity.this, WebActivity.class);
                intent.putExtra(WebActivity.INTENT_EXTRA_BEGIN_URL, "loansaduitpass");
                startActivity(intent);
                break;
            case R.id.personal_review_feedback_ll:
                intent = new Intent(PersonalActivity.this, WebActivity.class);
                intent.putExtra(WebActivity.INTENT_EXTRA_BEGIN_URL, "loansaduitrejectlist");
                startActivity(intent);
                break;
            case R.id.personal_wallet_balance_ll:
                intent = new Intent(PersonalActivity.this, WebActivity.class);
                intent.putExtra(WebActivity.INTENT_EXTRA_BEGIN_URL, "mypocket");
                startActivity(intent);
                break;
            case R.id.personal_wallet_withdraw_ll:
                intent = new Intent(PersonalActivity.this, WebActivity.class);
                intent.putExtra(WebActivity.INTENT_EXTRA_BEGIN_URL, "withdraw");
                startActivity(intent);
                break;
            case R.id.personal_wallet_income_ll:
                intent = new Intent(PersonalActivity.this, WebActivity.class);
                intent.putExtra(WebActivity.INTENT_EXTRA_BEGIN_URL, "benefitdetail");
                startActivity(intent);
                break;
            case R.id.personal_wallet_credit_ll:
                intent = new Intent(PersonalActivity.this, WebActivity.class);
                if (type == 1) {
                    intent.putExtra(WebActivity.INTENT_EXTRA_BEGIN_URL, "loanstabmain");
                } else if (type == 2) {
                    intent.putExtra(WebActivity.INTENT_EXTRA_BEGIN_URL, "loanstab");
                }
                startActivity(intent);
                break;
            case R.id.personal_my_order_ll:
                if (type == 1) {
                    intent = new Intent(PersonalActivity.this, WebActivity.class);
                    intent.putExtra(WebActivity.INTENT_EXTRA_BEGIN_URL, "orderaudit");
                    startActivity(intent);
                } else {
                    startActivity(new Intent(PersonalActivity.this, MyOrderActivity.class));
                }
                break;
            case R.id.personal_want_order_ll:
                if (type == 1) {
                    startActivity(new Intent(PersonalActivity.this, OrderListActivity.class));
                } else {
                    startActivity(new Intent(PersonalActivity.this, ClassifyActivity.class));
                }
//                intent = new Intent(PersonalActivity.this, WebActivity.class);
//                intent.putExtra(WebActivity.INTENT_EXTRA_BEGIN_URL, "questions");
//                startActivity(intent);
                break;
            case R.id.personal_good_manage_ll:
                intent = new Intent(PersonalActivity.this, WebActivity.class);
                intent.putExtra(WebActivity.INTENT_EXTRA_BEGIN_URL, "linelist");
                startActivity(intent);
                break;
            case R.id.personal_shop_inform_ll:
                intent = new Intent(PersonalActivity.this, WebActivity.class);
                intent.putExtra(WebActivity.INTENT_EXTRA_BEGIN_URL, "shopproductlist");
                startActivity(intent);
                break;
            case R.id.personal_add_proxy_ll:
                intent = new Intent(PersonalActivity.this, WebActivity.class);
                intent.putExtra(WebActivity.INTENT_EXTRA_BEGIN_URL, "addproxyshop");
                startActivity(intent);
                break;
            case R.id.personal_proxy_list_ll:
                intent = new Intent(PersonalActivity.this, WebActivity.class);
                intent.putExtra(WebActivity.INTENT_EXTRA_BEGIN_URL, "proxyshoplist");
                startActivity(intent);
                break;
            case R.id.personal_ordercar_ll:
                startActivity(new Intent(PersonalActivity.this, ShoppingCartActivity.class));
                break;
            case R.id.personal_address_ll:
                startActivity(new Intent(PersonalActivity.this, AddressActivity.class).putExtra("type",1));
                break;
            case R.id.personal_mycollection_ll:
                startActivity(new Intent(PersonalActivity.this, MineCollectActivity.class));
                break;
            case R.id.personal_sort_ll:
                startActivity(new Intent(PersonalActivity.this, ClassifyActivity.class));
                break;
            case R.id.personal_gift_ll:
                intent = new Intent(PersonalActivity.this, WebActivity.class);
                intent.putExtra(WebActivity.INTENT_EXTRA_BEGIN_URL, "share");
                startActivity(intent);
                break;
            case R.id.personal_phone_ll:
                intent = new Intent(PersonalActivity.this, WebActivity.class);
                intent.putExtra(WebActivity.INTENT_EXTRA_BEGIN_URL, "service");
                startActivity(intent);
                break;
            case R.id.personal_wait_pay_ll:
                intent = new Intent(PersonalActivity.this, OrderList2Activity.class).putExtra("page",1);
                startActivity(intent);
                break;
            case R.id.personal_wait_receive_ll:
                intent = new Intent(PersonalActivity.this, OrderList2Activity.class).putExtra("page",3);
                startActivity(intent);
                break;
            case R.id.personal_completed_ll:
                intent = new Intent(PersonalActivity.this, OrderList2Activity.class).putExtra("page",4);
                startActivity(intent);
                break;
            case R.id.personal_wait_shipments_ll:
                intent = new Intent(PersonalActivity.this, OrderList2Activity.class).putExtra("page",2);
                startActivity(intent);
                break;
            case R.id.setting:
                startActivity(new Intent(PersonalActivity.this, SettingActivity.class));
                break;
            case R.id.message:
                startActivity(new Intent(getBaseContext(),
                        MessageNoticeActivity.class));
                break;
        }
    }
}
